package cn.jsc.pet.controller;

import cn.jsc.basic.util.AjaxResult;
import cn.jsc.basic.util.PageList;
import cn.jsc.pet.domain.Pet;
import cn.jsc.pet.domain.PetDetail;
import cn.jsc.pet.query.PetQuery;
import cn.jsc.pet.service.IPetDetailService;
import cn.jsc.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "宠物接口",description = "宠物接口详细描述")
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @Autowired
    private IPetDetailService petDetailService;

    //获取一个对象：get
    //@GetMapping相当于@RequestMapping(value="/xxx",method=RequestMethod.GET)
    //Get http://localhost:8080/pet/1
    @ApiOperation(value = "查询单个宠物",notes = "需要传递id")
    @GetMapping("/{id}")//  pet/1    或 /pet/2
    public Pet findById(@PathVariable("id")Long id){
        return petService.getById(id);
    }


    //获取所有对象：get
    //Get http://localhost:8080/pet
    @ApiOperation(value = "查询所有",notes = "不需参数")
    @GetMapping
    public List<Pet> findAll(){
        return petService.getAll();
    }


    //分页 + 高级：post - 直接写，后面发送异步请求时再说
    //请求数据包中：请求行 + 请求头 + 请求体【参数就在请求体中】
    //Post http://localhost:8080/pet + json参数petQuery
    @ApiOperation(value = "分页查询宠物信息",notes = "需要传递当前页和每页显示的条数")
    @PostMapping
    public PageList<Pet> findPage(@RequestBody PetQuery petQuery){
        return petService.queryData(petQuery);
    }

    //删除  根据id删除  localhost:8080/dept/1
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        try {
            petService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    @PatchMapping//批量删除操作
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            petService.patchDelete(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    /**
     * /新增或修改  根据id来区分
     *  @RequestBody :前台请求体中的json数据会帮你转成对象
     *  localhost:8080/dept
     */
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet,HttpServletRequest request){
        try {
            if(pet.getId()==null){
                petService.add(pet,request);
            }else{
                petService.update(pet);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    //宠物上架
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    //宠物下架
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }
    /**
     * 根据宠物ID查询宠物详情
     * @return
     */
    @ApiOperation(value = "查询单个宠物",notes = "需要传递id")
    @GetMapping("/detail/{id}")//  pet/1    或 /pet/2
    public PetDetail findDetailByPetId(@PathVariable("id")Long id){
        return petDetailService.findDetailByPetId(id);
    }


    //管理员类型判断，让后台根据管理员类型判断是否显增删查
    @GetMapping("/isShopAdmin")
    public AjaxResult isShopAdmin(HttpServletRequest request){
        try {
            petService.isShopAdmin(request);
            return new AjaxResult();
        } catch (Exception e) {
//            e.printStackTrace();  //不能打印异常，只是用在做前台判断的
            return AjaxResult.me().setSuccess(false);
        }
    }
}
